<!--资讯发布-->
<template>
  <div>
    <a-card class="contentFullHeight"  :bodyStyle="{padding : '10px 10px'}">
      <!--筛选、导入导出-->
      <a-card :bordered="true" class="searchBox" :bodyStyle="{padding : '10px 24px'}">
        <a-form layout="inline">
          <a-form-item label="发布时间">
            <a-range-picker
              :defaultValue="[moment(searchParams.bgTm, dateFormat), moment(searchParams.endTm, dateFormat)]"
              :format="dateFormat"
              @change="onChange"
            ></a-range-picker>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" icon="search" @click="handleSearch">查询</a-button>
          </a-form-item>
          <a-form-item>
            <!--新增-->
            <a-button type="primary" icon="plus" @click="handleCreate">新增</a-button>
          </a-form-item>
        </a-form>
      </a-card>

      <!--列表-->
      <a-table :columns="columns" :dataSource="data"
               bordered size="small" rowKey="id"
               :pagination="pagination" :loading="loading"
               @change="handleTableChange"
      >

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleUpdate(record)" href="javascript:;">编辑</a>
            <a-divider type="vertical"/>
            <a href="javascript:;" @click="showConfirm(record)">删除</a>
          </template>
        </span>
      </a-table>
      <!--新增/编辑弹窗-->
      <InfoPopup :mdl="mdl" @handleSearch="handleSearch"></InfoPopup>
    </a-card>
  </div>
</template>

<script>
/* eslint-disable */
  import moment from 'moment';
  import InfoPopup from './infoPopup'
  import {queryInfoContentE,deleteInfoContentE} from '@/api/messageManage/newsDissemination'
  export default {
    components: {
      InfoPopup
    },
    data() {
      return {
        dateFormat: 'YYYY-MM-DD',
        /*   -------搜索框--------*/
        searchParams: {
          selected:'table_number',
          keywords:'',
          bgTm: moment(new Date()).subtract(6, 'days').format('YYYY-MM-DD'),
          endTm: this.$defaultSettings.endTm,
          addressId:null,
          serviceId:null,
          createBy: ''
        },

        /*   -------分页--------*/
        pagination: this.$defaultSettings.pagination ,

        /*   -------弹窗--------*/
        mdl: {
          dialogStatus: '',
          dialogTitleMap: {
            update: '编辑',
            create: '添加'
          },
          visible: false,
          title: '',
          formData: {
            bt: '',
            cjsj: '',
            dwdxid: '',
            dxid: '',
            fbr: '',
            gxsj: '',
            lxdxid: '',
            ly: '',
            nr: '',
            sh: '',
            tp: '',
            xwfbsj: ''
          },
          labelCol: {
            xs: {
              span: 24
            }
            ,
            sm: {
              span: 5
            }
          }
          ,
          wrapperCol: {
            xs: {
              span: 24
            }
            ,
            sm: {
              span: 16
            }
          },
          formItemLayoutWithOutLabel: {
            wrapperCol: {
              xs: { span: 24, offset: 0 },
              sm: { span: 20, offset: 4 },
            },
          },
        },


        /*   -------表格--------*/
        loading: false,
        columns:[
          {
            title: '资讯名称',
            dataIndex: 'bt'
          },
          {
            title: '资讯内容',
            dataIndex: 'nr'
          },
          {
            title: '发布时间',
            dataIndex: 'createTime'//xwfbsj
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: {customRender: 'action'}
          }
        ],
        data:[],

        // 下载
        downloadLoading: false,
      }
    },
    inject: ['upShow'],
    filters: {

    },
    created() {

    },
    computed: {

    },
    mounted() {
      this.getTableData()
    },
    methods: {
      moment,
      onChange(dates, dateStrings) {
        // console.log('From: ', dates[0], ', to: ', dates[1]);
        this.searchParams.bgTm = dateStrings[0];
        this.searchParams.endTm = dateStrings[1];
      },
      handleTableChange(pagination, filters, sorter) {
        this.pagination = pagination
        let sortOrder = ''
        let sortField = sorter.field ? sorter.field : ''
        if (sorter.order) {
          sortOrder = sortOrder === 'ascend' ? 'asc' : 'desc'
        }
        this.getTableData(sortField, sortOrder)
      },

      getTableData(sortField, sortOrder) {
        this.loading = true
        const parameter = {
          _search: true,//是否分页查询
         /* addressId: this.searchParams.addressId,
          alarm:0,*/
          bgTm:this.searchParams.bgTm,
          /*createBy:this.searchParams.createBy,*/
          /*data:{
            bgTm:"",
            endTm:"",
            keywords:"",
            noUpTime:"",
            status:"",
            type:"",
            userId:""
          },*/
          endTm:this.searchParams.endTm,
          keywords: this.searchParams.keywords,
          /*nd:"",*/
          pageNumber: this.pagination.current || 1,
          pageSize: this.pagination.pageSize || 10,
          selected: this.searchParams.selected,
          selectedType:"",
          sortField: sortField || '',
          sortOrder: sortOrder || '',
          /*status:"",*/
          type:""
        }
        console.log(parameter)
        queryInfoContentE(parameter).then(res => {
          this.loading = false
          this.pagination.total = res.data.total
          this.data = res.data.records
        }).
        catch(err => {
          this.loading = false
        })
      },
      handleSearch() {
        this.pagination.current = 1
        this.getTableData()
      },

      setDefaults() {
        this.mdl.formData = {
          bt: '',
          cjsj: '',
          dwdxid: '',
          dxid: '',
          fbr: '',
          gxsj: '',
          lxdxid: '',
          ly: '',
          nr: '',
          sh: '',
          tp: '',
          xwfbsj: ''
        }
      },
      //添加行
      handleCreate() {
        this.setDefaults()
        this.mdl.dialogStatus = 'create'
        this.mdl.visible = true
      },
      //编辑当前行
      handleUpdate(row) {
        console.log("这一行", row)
        this.mdl.formData = Object.assign({}, row) // copy obj
        this.mdl.dialogStatus = 'update'
        this.mdl.visible = true
      },

      handleDelete(row) {
        deleteInfoContentE(row.dxid).then(res => {
          if(res.code === 200)
          {
            this.upShow(1,res.msg || '删除成功')
            this.handleSearch()
          }
          else
          {
            this.upShow(2,res.msg)
          }

        }).
        catch(err => {
          this.upShow(2)
        })
      },
      showConfirm(val) {
        let self = this;
        this.$confirm({
          title: '删除该条资讯记录?',
          content: '确定删除该条资讯记录',
          onOk() {
            self.handleDelete(val)
          },
          onCancel() {},
        });
      },
      //下载
      handleDownload() {
        this.downloadLoading = true
        let param  ={
          sortField: '',
          sortOrder: '',
          keywords: this.searchParams.keywords,
          selected: this.searchParams.selected,
          addressId: this.searchParams.addressId,
          serviceId: this.searchParams.serviceId,
          bgTm:this.searchParams.bgTm,
          endTm:this.searchParams.endTm,
          createBy:this.searchParams.createBy,
        }
        refundRecorDdown(param).then(res=> {
          downloadFile(res,"退款记录","xlsx")
          this.downloadLoading = false
        })
      },

    }
  }
</script>

<style scoped>

</style>